package cn.dankal.flyaudio.personal;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.gyf.immersionbar.ImmersionBar;
import com.jeremyliao.liveeventbus.LiveEventBus;

import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import api.CurrentServiceFactory;
import api.UserServiceFactory;
import cn.dankal.basiclib.DKUserManager;
import cn.dankal.basiclib.base.activity.BaseActivity;
import cn.dankal.basiclib.pojo.UserInfoBean;
import cn.dankal.basiclib.rx.AbstractSubscriber;
import cn.dankal.basiclib.util.ToastUtils;
import cn.dankal.basiclib.util.image.PicUtils;
import cn.dankal.business.pojo.BaseBeanList;
import cn.dankal.business.pojo.CurrentBean;
import cn.dankal.flyaudio.FlyApplication;
import cn.dankal.flyaudio.R;
import cn.dankal.flyaudio.adapter.CurrentImgsAdapter;
import cn.dankal.flyaudio.dialog.AddFriendRequestSuccess;
import cn.dankal.flyaudio.jpush.JFriendManager;
import cn.jpush.im.android.api.ContactManager;
import cn.jpush.im.android.api.JMessageClient;
import cn.jpush.im.android.api.callback.GetUserInfoCallback;
import cn.jpush.im.android.api.model.UserInfo;
import cn.jpush.im.api.BasicCallback;

public class PersonalProfileActivity extends BaseActivity implements View.OnClickListener {


    private ImageView avatarView;

    private TextView nickNameView;

    private boolean isSelf = true;

    private TextView handleView;

    private TextView currentNumView;

    private TextView currentLikeNumView;

    private TextView publicName;

    private ImageView publicAvatar;

    private TextView publicTime;

    private TextView publicComment;

    private TextView publicZan;

    private RecyclerView imgs;

    private View publicView;

    private TextView publicContent;

    private TextView addressView;

    private TextView fansView;

    private String userUuid = "";
    private String userPhone = "";

    private UserInfo targetUserInfo = null;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_personal_profile;
    }

    @Override
    protected void initComponents() {
        ImmersionBar.with(this).init();
        userUuid = getIntent().getStringExtra("userId");
        String selfUuid = DKUserManager.getUserInfo().getUserUuid();
        if (TextUtils.isEmpty(userUuid)) {
            userUuid = selfUuid;
        }
        findViewById(R.id.iv_onback).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        isSelf = TextUtils.equals(userUuid, selfUuid);
        currentNumView = findViewById(R.id.tv_current_num);
        currentLikeNumView = findViewById(R.id.tv_current_like_num);
        avatarView = findViewById(R.id.iv_avatar);
        nickNameView = findViewById(R.id.tv_nickname);
        handleView = findViewById(R.id.tv_edit_profile);
        fansView = findViewById(R.id.tv_fans);
        handleView.setOnClickListener(this);
        imgs = findViewById(R.id.recyclerview);
        addressView = findViewById(R.id.tv_address);
        publicView = findViewById(R.id.cl_public);
        publicAvatar = findViewById(R.id.tv_public_avatar);
        publicName = findViewById(R.id.tv_public_name);
        publicComment = findViewById(R.id.tv_comment);
        publicZan = findViewById(R.id.tv_zan);
        publicContent = findViewById(R.id.tv_public_content);
        publicTime = findViewById(R.id.tv_public_time);
        JMessageClient.getUserInfo(userUuid, new GetUserInfoCallback() {
            @Override
            public void gotResult(int i, String s, UserInfo userInfo) {
                targetUserInfo = userInfo;
            }
        });

        LiveEventBus.get("update_user_info", String.class).observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                getFriendInfo();
            }
        });

        if (isSelf) {
            handleView.setText("编辑个人信息");
            handleView.setBackgroundResource(R.drawable.radius_aaadb6_stoke);
            handleView.setTextColor(getResources().getColor(R.color.color_2D343D));
            UserInfoBean userInfo = DKUserManager.getUserInfo();
            nickNameView.setText(userInfo.getName());
            addressView.setText(userInfo.getCarCity());
            PicUtils.loadAvatar(userInfo.getAvatar(), avatarView);
            currentLikeNumView.setText(userInfo.getUserInformationLikeNumber());
            currentNumView.setText(userInfo.getUserInformationCount() + "条发布");
            getUserInfo();
            requestCurrent();
        } else {
            UserServiceFactory.userInfoByUuid(userUuid)
                    .subscribe(new AbstractSubscriber<UserInfoBean>(this) {
                        @Override
                        public void onNext(UserInfoBean userInfoBean) {
                            nickNameView.setText(userInfoBean.getName());
                            PicUtils.loadAvatar(userInfoBean.getAvatar(), avatarView);
                            addressView.setText(userInfoBean.getCarCity());
                            currentLikeNumView.setText(userInfoBean.getUserInformationLikeNumber());
                            currentNumView.setText(userInfoBean.getUserInformationCount() + "条发布");
                            publicView.setVisibility(View.INVISIBLE);
                        }
                    });
            JFriendManager.isMyFriend(userUuid, new JFriendManager.GetMyFriendListener() {
                @Override
                public void isMyFriend(boolean isFriend) {
                    if (isFriend) {
                        handleView.setText("聊天");
                        handleView.setBackgroundResource(R.drawable.base_bottom_btn_radius24_5);
                        handleView.setTextColor(getResources().getColor(R.color.white));
                    } else {
                        handleView.setText("添加好友");
                        handleView.setBackgroundResource(R.drawable.base_bottom_btn_radius24_5);
                        handleView.setTextColor(getResources().getColor(R.color.white));
                    }
                }
            });

        }
    }


    @Override
    protected void onResume() {
        super.onResume();
        if (isSelf) return;
        JFriendManager.isMyFriend(userUuid, new JFriendManager.GetMyFriendListener() {
            @Override
            public void isMyFriend(boolean isFriend) {
                if (isFriend) {
                    handleView.setText("聊天");
                    handleView.setBackgroundResource(R.drawable.base_bottom_btn_radius24_5);
                    handleView.setTextColor(getResources().getColor(R.color.white));
                } else {
                    handleView.setText("添加好友");
                    handleView.setBackgroundResource(R.drawable.base_bottom_btn_radius24_5);
                    handleView.setTextColor(getResources().getColor(R.color.white));
                }
            }
        });

    }

    private void getFriendInfo() {
        UserServiceFactory.user_info()
                .subscribe(new AbstractSubscriber<UserInfoBean>(this) {
                    @Override
                    public void onNext(UserInfoBean userInfoBean) {
                        DKUserManager.updateUserInfo(userInfoBean);
                        nickNameView.setText(userInfoBean.getName());
                        PicUtils.loadAvatar(userInfoBean.getAvatar(), avatarView);
                        addressView.setText(userInfoBean.getCarCity());
                        currentLikeNumView.setText(userInfoBean.getUserInformationLikeNumber());
                        currentNumView.setText(userInfoBean.getUserInformationCount() + "条发布");
                    }
                });
    }

    private void getUserInfo() {
        if (DKUserManager.isLogined()) {
            UserServiceFactory.user_info()
                    .subscribe(new AbstractSubscriber<UserInfoBean>(this) {
                        @Override
                        public void onNext(UserInfoBean userInfoBean) {
                            DKUserManager.updateUserInfo(userInfoBean);
                            nickNameView.setText(userInfoBean.getName());
                            PicUtils.loadAvatar(userInfoBean.getAvatar(), avatarView);
                            addressView.setText(userInfoBean.getCarCity());
                            currentLikeNumView.setText(userInfoBean.getUserInformationLikeNumber());
                            currentNumView.setText(userInfoBean.getUserInformationCount() + "条发布");
                        }
                    });
        }
    }

    private void requestCurrent() {
        publicView.setVisibility(View.INVISIBLE);

        CurrentServiceFactory.user_info_list("1", "1")
                .subscribe(new AbstractSubscriber<BaseBeanList<CurrentBean>>(this) {
                    @Override
                    public void onNext(BaseBeanList<CurrentBean> currentBeanBaseBeanList) {
                        if (currentBeanBaseBeanList.list == null || currentBeanBaseBeanList.list.isEmpty()) {
                            return;
                        }
                        publicView.setVisibility(View.VISIBLE);
                        CurrentBean currentBean = currentBeanBaseBeanList.list.get(0);
                        PicUtils.loadCirclePic(PersonalProfileActivity.this, currentBean.avatar, publicAvatar);
                        publicName.setText(currentBean.name);
                        publicTime.setText(currentBean.createTime);
                        publicContent.setText(currentBean.content);
                        publicZan.setText(currentBean.likeNumber);
                        publicComment.setText(currentBean.commentNumber);
                        imgs.setLayoutManager(new GridLayoutManager(PersonalProfileActivity.this, 3));
                        CurrentImgsAdapter adapter = new CurrentImgsAdapter();
                        imgs.setAdapter(adapter);
                        fansView.setText("已有" + currentBean.likeNumber + "人点赞");
                        adapter.addDatas(currentBean.list, true);
                    }
                });
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (R.id.tv_edit_profile == id) {
            if (isSelf) {
                startActivity(new Intent(this, PersonalBaseProfileActivity.class));
            } else {
                if (handleView.getText().equals("聊天")) {
                    JFriendManager.toChat(targetUserInfo);
                } else {
                    doAddFriend();
                }
            }
        }
    }

    private void doAddFriend() {
        ContactManager.sendInvitationRequest(userUuid, FlyApplication.JPUSH_KEY, "我想加您为好友", new BasicCallback() {
            @Override
            public void gotResult(int responseCode, String responseMessage) {
                if (0 == responseCode) {
                    //好友请求请求发送成功
//                    ToastUtils.showShort("发送好友请求成功");
                    AddFriendRequestSuccess dialog = new AddFriendRequestSuccess();
                    dialog.show(getSupportFragmentManager(), "");

                } else {
                    //好友请求发送失败
                    ToastUtils.showShort("发送好友请求失败");
                }
            }
        });
    }
}
